<?php
/**
 * Created by PhpStorm.
 * User: zeopean
 * Date: 2015/9/15
 * Time: 17:05
 */

namespace App\Http\Controllers;

use App\Exceptions\SecurityException;
use App\Investor;
use App\InvestOrg;
use App\Project;
use App\User;
use App\UserTeams;
use Illuminate\Auth\Guard;

class AdminController extends Controller{

    public function __construct(Guard $guard)
    {
        parent::__construct();
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        $userId = $guard->user()->id;
        if(!User::isAdmins($userId))
            throw new SecurityException(SecurityException::NoPermission);
    }


    /**
     * 创业者列表页面
     * @return \Illuminate\View\View
     */
    public function getCreaterList(Guard $guard)
    {
        $creaters   = User::whereRole(0)->paginate(10);
        return view('admin.users.creater'  , array('creaters'=>$creaters ));
    }

    /**
     * 通过用户标识获取创业者信息
     * @param Guard $guard
     * @param $id
     * @return \Illuminate\View\View
     * @throws SecurityException
     */
    public function editCreaterPage(Guard $guard , $id)
    {
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        $creater    = User::find($id);
        return view('admin.users.edit_creater' , array('creater'=>$creater));
    }

    /**
     * 投资人列表页
     * @return \Illuminate\View\View
     */
    public function getInvestorList()
    {
        $investors  = User::whereRole(1)->paginate(10);
        if($investors)
            return view('admin.users.investor' , array('investors'=>$investors));
    }

    /**
     * 投资人信息编辑页面
     * @param Guard $guard
     * @param int $id
     * @return \Illuminate\View\View
     * @throws SecurityException
     */
    public function editInvestorPage(Guard $guard , $id=0)
    {
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        if($id != 0 )
            $investor   = User::find($id);
        return view('admin.users.edit_investor' , array('investor'=>$investor));

    }

    /**
     * 投资机构列表页
     * @return \Illuminate\View\View
     */
    public function getInvestOrgList()
    {
        $orgs = InvestOrg::paginate(10);
        return view('admin.users.invest_org' , ['orgs'=>$orgs]);
    }

    /**
     * 编辑投资机构
     * @param Guard $guard
     * @param int $id
     * @return \Illuminate\View\View
     * @throws SecurityException
     */
    public function editInvestOrgPage(Guard $guard ,  $id=0)
    {
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        if($id != 0)
            $investOrg = InvestOrg::find($id);
        else
            $investOrg = new InvestOrg();

        return view('admin.users.edit_invest_org' , ['investOrg'=>$investOrg]);

    }

    /**
     * 创业团队列表页
     * @return \Illuminate\View\View
     */
    public function getCreateTeamList()
    {
        $teams = UserTeams::paginate(10);
        return view('admin.users.create_team' , ['teams'=>$teams]);
    }

    /**
     * 系统管理员列表页
     * @return \Illuminate\View\View
     */
    public function getManagerList()
    {
        $admins = User::whereIsPower(3)->paginate(10);
        return view('admin.users.manager' ,['admins'=>$admins]);
    }


    /**
     * 获取项目列表
     * @return \Illuminate\View\View
     */
    public function getProjectsList()
    {
        $projects = Project::whereProjectAuth(1)->paginate(10);
        return view('admin.projects.projects' , array('projects'=>$projects));
    }

    /**创建新项目 或者编辑项目
     * @return \Illuminate\View\View
     */
    public function editProjectByProjectId(Guard $guard ,$id=0)
    {
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        if(Project::whereId($id)->exists()){
            $project = Project::find($id);
        }else{
            $project = new Project();
        }
        return view('admin.projects.edit_project' , ['project'=>$project]);
    }

    /**申请融资的项目列表
     * @return \Illuminate\View\View
     */
    public function getApplyFinanceProjectList()
    {
        $projects = Project::whereIsFinance(1)->paginate(10);
        return view('admin.projects.apply_finance_project' , ['projects'=>$projects]);
    }

    /**融资成功项目列表
     * @return \Illuminate\View\View
     */
    public function getSuccessFinanceProjectList()
    {
        $projects = Project::whereIsFinance(3)->paginate(10);
        return view('admin.projects.success_finance_project' , ['projects'=>$projects]);
    }

    /**
     * 审核投资人列表
     * @return \Illuminate\View\View
     */
    public function getCheckInvestorList()
    {
        $investors = Investor::paginate(10);
        return view('admin.checks.check_investors' , ['investors'=>$investors]);
    }

    /**
     * 审核投资机构列表
     * @return \Illuminate\View\View
     */
    public function getCheckInvestOrgList()
    {
        $orgs = InvestOrg::paginate(10);
        return view('admin.checks.check_invest_org' ,['orgs'=>$orgs]);
    }

    /**
     * 审核项目列表
     * @return \Illuminate\View\View
     */
    public function getCheckProjectsList()
    {
        $projects = Project::paginate(10);
        return view('admin.checks.check_projects' , ['projects'=>$projects]);
    }








}
